package com.vancouver.knows.ui.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.business.task.mark.FetchEventsTaskMark;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.ui.fragment.citydiscount.CityDiscountPullControllerView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/2.
 */
public abstract class LoadingFragment extends BaseFragment implements IResultReceiver{

    TextView errorBtn;
    View progress;
    FrameLayout content;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.loading_view, container, false);
        content = (FrameLayout) root.findViewById(R.id.content);
        errorBtn = (TextView) root.findViewById(R.id.errorBtn);
        progress = root.findViewById(R.id.progress);
        View contentView = createContentView(inflater);
        content.addView(contentView);
        return root;
    }

    protected void customizeLoadingView(ProgressBar progressBar) {
    }

    @Override
    public void onResume() {
        super.onResume();
        //判断任务状态
        updateLayout();
        ATaskMark aTaskMark = getTaskMark();
        if(aTaskMark.getTaskStatus() == ATaskMark.HANDLE_WAIT || aTaskMark.getTaskStatus() == ATaskMark.HANDLE_DOING ){
            loadData();
        }
    }

    @OnClick(R.id.errorBtn)
    public void OnErrorBtnClick(){
        loadData();
        updateLayout();
    }

    private void updateLayout(){
        //判断任务状态
        ATaskMark aTaskMark = getTaskMark();
        if(aTaskMark.getTaskStatus() == ATaskMark.HANDLE_WAIT || aTaskMark.getTaskStatus() == ATaskMark.HANDLE_DOING ){
            //ui显示
            content.setVisibility(View.GONE);
            errorBtn.setVisibility(View.GONE);
            progress.setVisibility(View.VISIBLE);

        }else if(aTaskMark.getTaskStatus() == ATaskMark.HANDLE_ERROR ){
            //ui显示
            content.setVisibility(View.GONE);
            errorBtn.setVisibility(View.VISIBLE);
            progress.setVisibility(View.VISIBLE);
        }else if(aTaskMark.getTaskStatus() == ATaskMark.HANDLE_OVER){
            //ui显示
            content.setVisibility(View.VISIBLE);
            errorBtn.setVisibility(View.GONE);
            progress.setVisibility(View.GONE);
        }
    }

    @Override
    public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
        updateLayout();
    }

    abstract protected ATaskMark getTaskMark();
    abstract protected void loadData();
    abstract protected View createContentView(LayoutInflater later);
}
